package com.itheima.controller;

import com.alibaba.fastjson.JSON;
import com.itheima.common.R;
import com.itheima.pojo.Site;
import com.itheima.service.SiteService;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 林晓伟
 */
@RestController
@RequestMapping("/addressBook")
public class SiteController {
    @Resource
    private SiteService siteService;

    /**
     * F08-设置默认收货地址
     */
    @CacheEvict(value = "aaaaaa",allEntries = true)
    @PutMapping("/default")
    public R alls(@RequestBody Site site) {
        Site alls = siteService.alls(site);
        if (alls != null) {
            return R.success(alls);
        }
        return R.error("");
    }

    /**
     * F08-获取默认收货地址
     */
    @CacheEvict(value = "aaaaaa",allEntries = true)
    @GetMapping("/default")
    public R all() {
        List<Site> all = siteService.all();
        if (all != null) {
            return R.success(all.get(0));
        }
        return R.error("");
    }

    /**
     * F09-获取收货地址列表
     */
    @Cacheable(value = "aaaaaa")
    @GetMapping("/list")
    public R alll() {
        List<Site> alll = siteService.alll();
        return R.success(alll);
    }

    /**
     * F09-根据id获取收货地址
     */
    @CacheEvict(value = "aaaaaa",allEntries = true)
    @GetMapping("/{id}")
    public R find(@PathVariable String id) {
        Site site = siteService.findbyId(id);
        if (site != null) {
            return R.success(site);
        }
        return R.error("");
    }

    /**
     * F10-添加收货地址
     */
    @CacheEvict(value = "aaaaaa",allEntries = true)
    @PostMapping
    public R all(@RequestBody Site site) {
        Site aad = siteService.aad(site);
        if (aad != null) {
            return R.success(aad);
        }
        return R.error("添加失败");
    }

    /**
     * F11-删除收货地址
     */
    @CacheEvict(value = "aaaaaa",allEntries = true)
    @DeleteMapping
    public R delete(@RequestParam String ids) {
        Integer delete = siteService.delete(ids);
        if (delete != 0) {
            return R.success("删除成功");
        }
        return R.error("删除失败");
    }

    /**
     * F12-修改收货地址
     */
    @CacheEvict(value = "aaaaaa",allEntries = true)
    @PutMapping
    public R update(@RequestBody Site site) {
        Integer update = siteService.update(site);
        if (update != 0) {
            return R.success("修改成功");
        }
        return R.error("修改失败");
    }


}
